package com.xb.cloud.shopping.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.xb.cloud.common.constant.DbConstants;
import com.xb.cloud.common.model.OrderStatus;
import com.xb.cloud.order.service.client.bo.SellOrderBo;
import com.xb.cloud.order.service.client.bo.SellOrderItemBo;
import com.xb.cloud.product.merchant.service.client.bo.ShopSkuBo;
import com.xb.cloud.product.service.client.CommonAddressService;
import com.xb.cloud.shopping.client.vo.CheckOutInputVo;
import com.xb.cloud.shopping.model.ShoppingCartItem;
import com.xb.cloud.shopping.service.OrderSplitService;
import com.xb.cloud.user.service.client.bo.EndUserAddressBo;


@Service
public class OrderSplitServiceImpl implements OrderSplitService {

	@Resource
	CommonAddressService commonAddressService;

	public List<SellOrderBo> splitOrder(CheckOutInputVo cartCheckOutVo, Map<Long, ShopSkuBo> skuMap,
			EndUserAddressBo selectedAddress) {

		List<ShoppingCartItem> itemList = cartCheckOutVo.getItemList();
		// 将下单的商品根据不同的店铺分组
		List<Long> skuIdList = new ArrayList<>();
		Map<Long, List<ShoppingCartItem>> shopCartItemsMap = new HashMap<>();

		for (ShoppingCartItem shoppingCartItem : itemList) {
			Long skuId = shoppingCartItem.getSkuId();
			skuIdList.add(skuId);// 商品sku的id列表
			ShopSkuBo shopSkuBo = skuMap.get(skuId);// 获取sku信息
			Long shopId = shopSkuBo.getShopId();

			List<ShoppingCartItem> shopItems = shopCartItemsMap.get(shopId);
			if (CollectionUtils.isEmpty(shopItems)) {
				shopItems = new ArrayList<>();
				shopItems.add(shoppingCartItem);
				shopCartItemsMap.put(shopId, shopItems);
			} else {
				shopItems.add(shoppingCartItem);
			}
		}

		long companyId = cartCheckOutVo.getCompanyId();
		long endUserId = cartCheckOutVo.getCurrentUserId();
		String receiverName = cartCheckOutVo.getReceiverName();
		String receiverPhone = cartCheckOutVo.getReceiverPhone();
		String remark = cartCheckOutVo.getRemark();
//		Long countyId = selectedAddress.getCountyId();
//		AddressBo detailAddress = commonAddressService.getDetailsByCountyId(countyId);

		Set<Long> shopSet = shopCartItemsMap.keySet();
		SellOrderBo parentOrder = null;
		if (shopSet.size() > 1) {
			// 有多家店铺，需要拆单
			parentOrder = new SellOrderBo();
			parentOrder.setCompanyId(companyId);
			parentOrder.setEndUserId(endUserId);
			parentOrder.setIsLeaf(DbConstants.FALSE);
		}

		List<SellOrderBo> orderList = new ArrayList<>();
		// 根据店铺拆单
		for (Long shopId : shopSet) {
			Long merchantId = null;
			List<SellOrderItemBo> orderItemList = new ArrayList<>();// 订单商品列表
			BigDecimal totalSkuAmount = BigDecimal.valueOf(0);// 商品总价格

			List<ShoppingCartItem> cartItems = shopCartItemsMap.get(shopId);// 根据店铺获取购物车商品列表
			for (ShoppingCartItem shoppingCartItem : cartItems) {
				SellOrderItemBo orderItem = new SellOrderItemBo();
				orderItem.setCompanyId(companyId);
				orderItem.setEndUserId(endUserId);
				orderItem.setShopId(shopId);
				orderItem.setIsLeaf(DbConstants.TRUE);// 暂时没有组合品

				Long skuId = shoppingCartItem.getSkuId();
				ShopSkuBo shopSkuBo = skuMap.get(skuId);// 获取sku信息
				merchantId = shopSkuBo.getMerchantId();

				BigDecimal priceSale = shopSkuBo.getPriceSale();// 商品价格
				int num = shoppingCartItem.getNum();// 商品下单数量
				BigDecimal skuAmount = priceSale.multiply(BigDecimal.valueOf(num));// sku总价
				totalSkuAmount = totalSkuAmount.add(skuAmount);

				// sku info
				orderItem.setMerchantId(merchantId);
				orderItem.setSkuId(skuId);
				orderItem.setSkuNameCn(shopSkuBo.getSkuNameCn());
				orderItem.setSkuPrice(priceSale);
				orderItem.setSkuNum(num);
				orderItem.setSkuAmount(skuAmount);

				// product info
				orderItem.setProductId(shopSkuBo.getProductId());
				orderItem.setProductPicUrl(shopSkuBo.getDefaultPicURL());
				// 实际冻结库存=商品下单数量
				orderItem.setFrozenRealStockNum(num);

				orderItemList.add(orderItem);
			}

			SellOrderBo order = new SellOrderBo();
			order.setCompanyId(companyId);
			order.setEndUserId(endUserId);
			order.setMerchantId(merchantId);
			order.setShopId(shopId);
			order.setSoStatus(OrderStatus.PENDING_PAYMENT.getCode());// 未支付
			order.setIsLeaf(DbConstants.TRUE);
			order.setSkuAmount(totalSkuAmount);
			order.setOrderAmount(totalSkuAmount);// 一期没有运费等
			order.setSoUserRemark(remark);
			order.setBusinessType(DbConstants.ORDER_BUS_TYPE_USER);

			order.setGoodReceiverName(receiverName);
			order.setGoodReceiverPhone(receiverPhone);
//			order.setGoodReceiverCountryId(1L);// 中国
//			order.setGoodReceiverProvinceId(selectedAddress.getProvinceId());
//			order.setGoodReceiverProvince(detailAddress.getProvinceName());
//			order.setGoodReceiverCityId(selectedAddress.getCityId());
//			order.setGoodReceiverCity(detailAddress.getCityName());
//			order.setGoodReceiverCountyId(countyId);
//			order.setGoodReceiverCounty(detailAddress.getCountyName());
//			order.setGoodReceiverAddress(selectedAddress.getAddress());

			order.setItems(orderItemList);

			if (parentOrder != null) {
				order.setParentOrder(parentOrder);
			}

			orderList.add(order);
		}

		return orderList;
	}

}
